//
//  Encryption.m
//

#import "Encryption.h"
#define Encry @"com.ayf"//  拼接参数
#import "CommonCrypto/CommonDigest.h"

@implementation Encryption
+(NSDictionary *)strencryption:(NSDictionary *)dic//直接调用这个就好
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    NSArray * arraykey =dic.allKeys;
    for (int i=0; i<arraykey.count; i++) {
        [dict setObject:dic[arraykey[i]] forKey:arraykey[i]];
    }
    NSArray *keys = [dict allKeys];
    
    NSArray *sortedArray = [keys sortedArrayUsingSelector:@selector(compare:)];
    
    NSString * str=[[NSString alloc]init];
    for (NSString *categoryId in sortedArray)
    {
        str =[str stringByAppendingString:[dict objectForKey:categoryId]];
    }
    str = [str stringByAppendingString:Encry];// 加，这是我们这拼接的
    NSMutableDictionary * returndic = [NSMutableDictionary dictionaryWithDictionary:dic];
    [returndic setObject:[Encryption md5:str] forKey:@"token"];

   
    return   returndic;
}
 //32位md5加密
+ (NSString *)md5:(NSString *)str
{
   const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr,strlen(cStr), result);
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
+ (BOOL)isjianshufloatorint:(NSString*)candidate
{
    NSString* Regex = @"([0]|([1-9][0-9]{0,7}))(\\.[0-9]{1,2})?";
    NSPredicate* Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
 
    
    return [Test evaluateWithObject:candidate];
}
+ (BOOL)iszhenzhengshu:(NSString*)candidate
{

    NSString* Reges = @"^[0-9]*[1-9][0-9]*$";
    NSPredicate* Tests = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Reges];
    return [Tests evaluateWithObject:candidate]&&candidate.length<=9;
}
@end
